/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.app;

import java.awt.Font;

import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

/**
 * Look and feel related methods.
 * <p>
 * Default look and feel and font used by the application. 
 *   
 * @author Francisco Morero Peyrona
 */
final class LookAndFeel
{
   /**
    * Changes font size for all visible components.
    * 
    * @param nSize
    */
   void setFontSize( int nSize )
   {
      FontUIResource font = new FontUIResource( "Dialog", Font.PLAIN, nSize );
      
      UIManager.put( "Label.font"        , font );
      UIManager.put( "TextField.font"    , font );
      UIManager.put( "TextArea.font"     , font );
      UIManager.put( "Spinner.font"      , font );
      UIManager.put( "PasswordField.font", font );
      UIManager.put( "Button.font"       , font );
      UIManager.put( "RadioButton.font"  , font );
      UIManager.put( "CheckBox.font"     , font );
      UIManager.put( "ComboBox.font"     , font );
      UIManager.put( "Menu.font"         , font );
      UIManager.put( "List.font"         , font );
      UIManager.put( "MenuItem.font"     , font );
      UIManager.put( "Panel.font"        , font );
      UIManager.put( "Table.font"        , font );
      UIManager.put( "TableHeader.font"  , font );
      UIManager.put( "TitledBorder.font" , font );
      UIManager.put( "Tree.font"         , font );
      UIManager.put( "TabbedPane.font"   , font );
      
      /* ANOTHER WAY TO DO IT (now for all components):
      // Eample: setUIFont( new javax.swing.plaf.FontUIResource( "Dialog", Font.PLAIN, nSize ) ); 
      public static void setUIFont( javax.swing.plaf.FontUIResource f )
      {
         java.util.Enumeration keys = UIManager.getDefaults().keys();
         while (keys.hasMoreElements()) 
         {
            Object key = keys.nextElement();
            Object value = UIManager.get (key);
            if( value instanceof javax.swing.plaf.FontUIResource )
               UIManager.put (key, f);
         }
      }*/      
   }
   
   /**
    * Set look and feel based.
    * 
    * @param sLF Look and Feel name to be sat. 
    * @throws Exception If could not be sat.
    */
   void setLookAndFeel( String sLF ) throws Exception
   {
      boolean bUseDefault = true;
      
      if( sLF.length() > 0  )   // There is one specified by user  
      {  // TODO: Cuando se escoje utilizar el L&F de Windows bajo Linux da un error y no se ve nada
         // Get available LAFs
         UIManager.LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();
         
         // Selects the choosen one
         for( int n = 0; n < lafInfo.length; n++ )
         {
            if( lafInfo[n].getName().equalsIgnoreCase( sLF ) )
            {
               try
               {
                  UIManager.setLookAndFeel( lafInfo[n].getClassName() );
                  bUseDefault = false;
               }
               catch( ClassNotFoundException exc )
               {
                  App.showMessage( "Look & Feel problem", "Can't load \""+ sLF +"\" Look and Feel" );
               }
            }
         }
      }
      
      if( bUseDefault )      // Set default OS L&F
      {
         try
         {
            UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );  
         } 
         catch( Exception exc )
         {
            App.showMessage( "Look & Feel problem",
                             "Unable to load native look and feel.\nUsing cross platform Look and Feel" );
            try
            { 
               UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
            }
            catch( Exception e )
            {
               App.showMessage( "Look & Feel error",
                                "Unable to load Look and Feel.\nSorry, but your Java is totally fucked up:\n"+
                                "download a new JRE from 'http://www.java.sun.com'" );
               App.halt( e, App.nERR_LOOK_AND_FEEL );
            }
         }
      }
   }
}